package cn.wonderland.pet.service.impl;

import cn.wonderland.basic.util.BaiduAuditUtils;
import cn.wonderland.basic.util.LoginContext;
import cn.wonderland.org.domain.Employee;
import cn.wonderland.org.domain.Shop;
import cn.wonderland.org.mapper.EmployeeMapper;
import cn.wonderland.pet.domain.Pet;
import cn.wonderland.pet.domain.PetDetail;
import cn.wonderland.pet.domain.PetOnlineAuditLog;
import cn.wonderland.pet.mapper.PetDetailMapper;
import cn.wonderland.pet.mapper.PetMapper;
import cn.wonderland.pet.mapper.PetOnlineAuditLogMapper;
import cn.wonderland.pet.service.IPetService;
import cn.wonderland.basic.service.impl.BaseServiceImpl;
import cn.wonderland.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wonderland
 * @since 2023-05-13
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper detailMapper;

    @Autowired
    private PetOnlineAuditLogMapper logMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void add(Pet pet) {
        // 保存宠物基本信息 简单玩法设置shopId
//        Logininfo logininfo = LoginContext.getLogininfo();
//        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        Shop shop = LoginContext.getUserInfo().getShop();
        pet.setShopId(shop.getId());
        pet.setState(0);
        pet.setCreatetime(new Date());
        super.add(pet);
        // 添加pet的详情信息
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        detailMapper.add(detail);

    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        detailMapper.update(detail);

    }

    /**
     * 上架
     *
     * @param ids
     */
    @Override
    @Transactional // 1,2,3 上架
    public void onsale(List<Long> ids) {
        // 参数校验
        // 1.过滤掉已上架的数据 返回的都是下架的了
        List<Pet> petList = petMapper.findByIds(ids);
        if (CollectionUtils.isEmpty(petList)) {
            return;
        }
        // 2.审核--百度AI审核 stream 流的foreach里面 return 是 进行下一次循环
        List<PetOnlineAuditLog> logList = new ArrayList<>();
        List<Long> petIdList = new ArrayList<>();
        petList.forEach(pet -> {
            StringBuilder sb = new StringBuilder();
            // 名称
            if (!BaiduAuditUtils.TextCensor(pet.getName())) {
                sb.append("名称审核不通过：").append(pet.getName());
            }
            // resources
//            String[] split = pet.getResources().split(",");
//            Arrays.stream(split).forEach(res -> {
//                if (!BaiduAuditUtils.ImgCensor(res)) {
//                    sb.append("图片审核不通过：").append(res);
//                }
//            });
            if (sb.length() > 0) {
                // 3.审核失败 记录日志
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setPetId(pet.getId());
                auditLog.setState(0);
                auditLog.setAuditTime(new Date());
                auditLog.setNote(sb.toString());
                auditLog.setAuditId(-1L);
//                logMapper.add(auditLog);
//                return;
                logList.add(auditLog);
                return;
            }
            petIdList.add(pet.getId());
        });
        // 批量添加 审核日志
        if (!CollectionUtils.isEmpty(logList)) {
            logMapper.batchAdd(logList);
        }
        // 批量修改状态 状态+上架时间
        if (!CollectionUtils.isEmpty(petIdList)) {
            petMapper.batchUpdate(petIdList);
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        petMapper.batchUpdateOff(ids);
    }
}
